package li.teng.android.xplanet.content;

import java.util.Date;

import li.teng.android.xplanet.XplanetConstants;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;

public class MyPositionDBAdapter {
  private static final String DATABASE_NAME = "goxplanet.db";
  private static final String DATABASE_TABLE = "positions";
  private static final int DATABASE_VERSION = 1;
 
  public static final String KEY_ID = "id";
  public static final String KEY_NAME = "name";
  public static final String KEY_LOGITUDE = "logitude";
  public static final String KEY_LATITUDE = "latitude";

  
  private SQLiteDatabase db;
  private final Context context;
  private MyPositionDBOpenHelper dbHelper;

  public MyPositionDBAdapter(Context _context) {
    this.context = _context;
    dbHelper = new MyPositionDBOpenHelper(context, DATABASE_NAME, 
                                    null, DATABASE_VERSION);
  }
  
  public void close() {
    db.close();
  }
  
  public void open() throws SQLiteException {  
    try {
      db = dbHelper.getWritableDatabase();
    } catch (SQLiteException ex) {
      db = dbHelper.getReadableDatabase();
    }
  }  
  
  //Insert a new task
  public long insert(MyPosition _position) {
    // Create a new row of values to insert.
    ContentValues newTaskValues = new ContentValues();
    // Assign values for each row.
//    newTaskValues.put(KEY_ID, _position.getId());
    newTaskValues.put(KEY_NAME, _position.getName());
    newTaskValues.put(KEY_LOGITUDE, _position.getLogitude());
    newTaskValues.put(KEY_LATITUDE, _position.getlatitude());
    // Insert the row.
    return db.insert(DATABASE_TABLE, null, newTaskValues);
  }

  // Remove a task based on its index
  public boolean removeTask(long _rowIndex) {
    return db.delete(DATABASE_TABLE, KEY_ID + "=" + _rowIndex, null) > 0;
  }

  // Update a task
  public boolean updateTask(long _rowIndex, String _task) {
//    ContentValues newValue = new ContentValues();
//    newValue.put(KEY_TASK, _task);
//    return db.update(DATABASE_TABLE, newValue, KEY_ID + "=" + _rowIndex, null) > 0;
	  return true;
  }
  
  public Cursor getAllToDoItemsCursor() {
	Cursor c = db.query(DATABASE_TABLE, 
              new String[] { KEY_ID, KEY_NAME, KEY_LOGITUDE,KEY_LATITUDE}, 
              null, null, null, null, null);
	Log.d(XplanetConstants.TAG, "getAllToDoItemsCursor = " + c);
    return c;
  }

  public Cursor setCursorToToDoItem(long _rowIndex) throws SQLException {
    Cursor result = db.query(true, DATABASE_TABLE, 
	                           new String[] {KEY_ID, KEY_NAME, KEY_LOGITUDE,KEY_LATITUDE},
                             KEY_ID + "=" + _rowIndex, null, null, null, 
                             null, null);
    if ((result.getCount() == 0) || !result.moveToFirst()) {
      throw new SQLException("No to do items found for row: " + _rowIndex);
    }
    return result;
  }

  public MyPosition getMyPosition(long _rowIndex) throws SQLException {
    Cursor cursor = db.query(true, DATABASE_TABLE, 
                             new String[] {KEY_ID, KEY_NAME, KEY_LOGITUDE,KEY_LATITUDE},
                             KEY_ID + "=" + _rowIndex, null, null, null, 
                             null, null);
    if ((cursor.getCount() == 0) || !cursor.moveToFirst()) {
      throw new SQLException("No to do item found for row: " + _rowIndex);
    }

    String task = cursor.getString(cursor.getColumnIndex(KEY_NAME));
    double logitude = cursor.getDouble(cursor.getColumnIndex(KEY_LOGITUDE));
    double latitude = cursor.getDouble(cursor.getColumnIndex(KEY_LATITUDE));
		  
    MyPosition result = new MyPosition(_rowIndex,task,logitude, latitude);
    return result;  
  }
  
  private static class MyPositionDBOpenHelper extends SQLiteOpenHelper {

	  public MyPositionDBOpenHelper(Context context, String name,
	                          CursorFactory factory, int version) {
	    super(context, name, factory, version);
	  }

	  // SQL Statement to create a new database.
	  private static final String DATABASE_CREATE = "create table " + 
	    DATABASE_TABLE + " (" + KEY_ID + " integer primary key autoincrement, " +
	    KEY_NAME + " text not null, " + 
	    KEY_LOGITUDE + " double , " + 
	    KEY_LATITUDE + " double );" ;

	  @Override
	  public void onCreate(SQLiteDatabase _db) {
	    _db.execSQL(DATABASE_CREATE);
	  }

	  @Override
	  public void onUpgrade(SQLiteDatabase _db, int _oldVersion, int _newVersion) {
	    Log.w("TaskDBAdapter", "Upgrading from version " + 
	                           _oldVersion + " to " +
	                           _newVersion + ", which will destroy all old data");

	    // Drop the old table.
	    _db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
	    // Create a new one.
	    onCreate(_db);
	  }
	}
}
